<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 广告控制器
 * @author wb_wenjie.wuwj <wb_wenjie.wuwj@alibaba-inc.com>
 * @copyright 2013-2-26
 */

class Ad extends MY_Controller
{
	function __construct()
	{
		parent::__construct();
		
		// 后台登录验证
		$this->check_login();
		
		// 验证权限
		$this->verify_purview(PURVIEW_AD);
		
		$this->load->model('ad_m');
		
		// 加载广告的语言包
		$this->load->language('ad');
		$this->validate_data = 
				array('customer' => array(
										array('field' => 'customer', 'label' => 'lang:ad_customer_name', 'rules' => 'trim|required|max_length[20]|xss_clean'),
										array('field' => 'placeid', 'label' => 'lang:ad_place', 'rules' => 'trim|required|is_natural_no_zero'),
										array('field' => 'adtype', 'label' => 'lang:ad_type', 'rules' => 'trim|required'),
										array('field' => 'link', 'label' => 'lang:ad_link', 'rules' => 'trim|max_length[200]'),
										array('field' => 'path', 'label' => 'lang:com_title_upload_image', 'rules' => 'trim|max_length[200]'),
										array('field' => 'begintime', 'label' => 'lang:ad_time', 'rules' => 'trim|required|max_length[10]'),
										array('field' => 'finishtime', 'label' => 'lang:ad_time', 'rules' => 'trim|required|max_length[10]'),
										array('field' => 'isactive', 'label' => 'lang:ad_isactive', 'rules' => 'trim|required|is_natural'),
										array('field' => 'remark', 'label' => 'lang:com_remark', 'rules' => 'trim|max_length[20]')
										),
						'place' =>array(
										array('field' => 'place', 'label' => 'lang:ad_place', 'rules' => 'trim|required|max_length[20]|xss_clean'),
										array('field' => 'explain', 'label' => 'lang:ad_explain', 'rules' => 'trim|max_length[100]'),
										array('field' => 'price', 'label' => 'lang:ad_price', 'rules' => 'trim|required|is_natural|max_length[20]'),
										array('field' => 'height', 'label' => 'lang:ad_size', 'rules' => 'trim|required|is_natural_no_zero'),
										array('field' => 'width', 'label' => 'lang:ad_size', 'rules' => 'trim|required|is_natural_no_zero'),
										array('field' => 'explain', 'label' => 'lang:ad_explain', 'rules' => 'trim|max_length[100]'),
										array('field' => 'isactive', 'label' => 'lang:ad_isactive', 'rules' => 'trim|required|is_natural'),
										array('field' => 'displaystyle', 'label' => 'lang:ad_display_style', 'rules' => 'trim|required|max_length[10]'),
										array('field' => 'remark', 'label' => 'lang:com_remark', 'rules' => 'trim|max_length[20]')
										)
					);
	}
	
	/**
	 * 广告位列表
	 */
	function place_manage()
	{
		// 查询广告
		$ad_place      = $this->ad_m->show_ad_place();
	
		// 广告信息列表数组
		$i = 1;
		foreach ($ad_place as $key => $row)
		{
			$ad_place[$key]['ordernum'] 	= $i;
			$ad_place[$key]['isactive'] 	= $row['isactive'] == 0 ? $this->lang->line('com_sign_inactive') : $this->lang->line('com_sign_active');
			$i++;
		}

		$data['header'] 		= $this->header;
		$data['title']			= $this->lang->line('ad_title_place_list');
		$data['page_label']		= $this->lang->line('ad_page_label');
		$data['nav_label'] 		= $this->lang->line('com_navigate_label');
		$data['ad_place'] 	= $ad_place;
		$this->load->view('ad_place_list',$data);
	}
	
	/**
	 * 显示广告位置添加页面
	 */
	function place_add()
	{
		$data['header'] 		= $this->header;
		$data['title']			= $this->lang->line('ad_title_place_add');
		$data['page_label']		= $this->lang->line('ad_page_label');
		$data['nav_label'] 		= $this->lang->line('com_navigate_label');
		$data['loginname'] 		= $this->loginname;
		$this->load->view('ad_place_add', $data);
	}
	
	/**
	 * 广告位添加业务处理
	 */
	function place_add_action()
	{
		$validate = $this->validation($this->validate_data['place']);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('ad/customer_add'), $validate, 'goback');
		}
	
		$ad = $this->input->post();
		
		if ($ad && isset($ad['s_ad_place_add']))
		{
			// 清除数组中无用的数据，添加相关信息
			unset($ad['s_ad_place_add']);
			
			// 添加信息
			$result = $this->ad_m->insert_ad_place($ad);
			if ($result != FALSE)
			{
				$this->_show_message(base_url('ad/place_manage'), $this->lang->line('msg_operation_success'));
			}
			else
			{
				$this->_show_message(base_url('ad/palce_add'), $this->lang->line('msg_operation_failed'), 'goback');
			}
		}
		else
		{
			$this->_show_message(base_url('ad/palce_add'), $this->lang->line('error_invalid_post'), 'goback');
		}
	}
	
	/**
	 * 显示广告位编辑页面
	 * @param int $id 广告位id
	 */
	function place_edit($id)
	{
		$place_id['id'] = intval($id);
		if (empty($place_id))
		{
			$this->_show_message(base_url('ad/place_manage'), $this->lang->line('ad_placeid_unable_empty'), 'goback');
		}
		
		// 查询广告位置信息
		$place = $this->ad_m->get_ad_place($place_id);
		if (empty($place))
		{
			$this->_show_message(base_url('ad/place_manage'), $this->lang->line('ad_place_does_not_exist'), 'goback');
		}
		$place = $place[0];
		$data = array (
			
				'header' => $this->header,
				'ad_place' => $place,
				'title' => $this->lang->line('ad_title_place_edit'),
				'page_label' => $this->lang->line('ad_page_label'),
				'nav_label' => $this->lang->line('com_navigate_label') 
		);
		$this->load->view('ad_place_edit', $data);
	}
	
	/**
	 * 广告位编辑业务处理
	 */
	function place_edit_action()
	{
		$validate = $this->validation($this->validate_data['place']);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('ad/customer_add'), $validate, 'goback');
		}
	
		// 获取表单数据并更新
		$ad_place = $this->input->post();
		if ($ad_place && isset($ad_place['s_ad_place_edit']))
		{
			$ad_place_id['id'] = intval($ad_place['id']);
			unset($ad_place['s_ad_place_edit']);
			unset($ad_place['id']);
			$result = $this->ad_m->update_ad_place($ad_place_id, $ad_place);
			$this->_show_message(base_url('ad/place_manage'), $this->lang->line('msg_operation_success'));
		}
	}
	
	/**
	 * ajax删除广告位
	 */
	function place_delete_action()
	{
		// 对表单数据做判断
		$validate = $this->validation(array(array('field' => 'id[]', 'label' => 'lang:ad_customer_id', 'rules' => 'trim|required|is_natural|xss_clean')));
		if (TRUE !== $validate)
		{
			echo $this->array2json(TRUE, $validate);
			return ;
		}
	
		// 获取广告位置ID值，并放入数组
		$id['id'] = $this->input->post('id');
		if ($this->ad_m->delete_ad_place($id))
		{
			echo $this->array2json(FALSE, $this->lang->line('msg_operation_success'));
			return ;
		}
		else
		{
			echo $this->array2json(TRUE, $this->lang->line('msg_operation_failed'));
			return ;
		}
	}
	
	/**
	 * 广告商列表显示页面
	 * @param int $page
	 */
	function customer_manage($page = '')
	{
		// 获取要显示的页数
		$page = empty($page) ? 1 : abs(intval($page));
		
		// 组织分页信息
		$config['base_url']		= base_url('ad/customer_manage');
		$config['total_rows']	= $this->ad_m->get_ad_customer_count();
		
		$page_data 		= $this->get_page($config);
		$data['page'] 	= $page_data['page'];

		// 查询广告商
		$customer      = $this->ad_m->show_ad_customer($page_data['limit']);
		
		// 广告商列表数组
		$i = 1;
		foreach ($customer as $key => $row)
		{
			$customer[$key]['isactive'] = $row['isactive'] == 1 ? $this->lang->line('com_sign_active') : $this->lang->line('com_sign_inactive');
			$customer[$key]['ordernum'] = ($page - 1) * $page_data['per_page'] + $i;
			$customer[$key]['begintime'] = date(DATE_FORMAT_AD,$row['begintime']);
			$customer[$key]['finishtime'] = date(DATE_FORMAT_AD, $row['finishtime']);
			$i++;
		}
		
		$data['header'] 		= $this->header;
		$data['ad_customer']	= $customer;
		$data['title']			= $this->lang->line('ad_title_customer_list');
		$data['page_label']		= $this->lang->line('ad_page_label');
		$data['nav_label'] 		= $this->lang->line('com_navigate_label');
		$this->load->view('ad_customer_list',$data);
	}
	
	/**
	 * 显示广告商添加页面
	 */
	function customer_add()
	{
		$data['header'] 		= $this->header;
		$data['title']			= $this->lang->line('ad_title_customer_add');
		$data['page_label']		= $this->lang->line('ad_page_label');
		$data['nav_label'] 		= $this->lang->line('com_navigate_label');
		$data['loginname'] 		= $this->loginname;
		$data['place'] 			= $this->get_ad_place_as_option();
		$this->load->view('ad_customer_add', $data);
	}
	
	/**
	 * 广告商添加业务处理
	 */
	function customer_add_action()
	{
		$validate = $this->validation($this->validate_data['customer']);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('ad/customer_add'), $validate, 'goback');
		}
	
		$customer = $this->input->post();
	
		if ($customer && isset($customer['s_ad_customer_add']))
		{
			// 清除数组中无用的数据，添加相关信息
			unset($customer['s_ad_customer_add']);
			
			// 判断添加的时间，结束时间要大于开始时间
			$customer['begintime'] = strtotime($customer['begintime']);
			$customer['finishtime'] = strtotime($customer['finishtime']);
			if ($customer['begintime'] >= $customer['finishtime'])
			{
				$this->_show_message(base_url('ad/customer_add'), $this->lang->line('ad_error_time'), 'goback');
			}
			
			// 添加信息
			$result = $this->ad_m->insert_ad_customer($customer);
			if ($result != FALSE)
			{
				$this->_show_message(base_url('ad/customer_manage'), $this->lang->line('msg_operation_success'));
			}
			else
			{
				$this->_show_message(base_url('ad/customer_add'), $this->lang->line('msg_operation_failed'), 'goback');
			}
		}
		else
		{
			$this->_show_message(base_url('ad/customer_add'), $this->lang->line('error_invalid_post'), 'goback');
		}
	}
	
	/**
	 * 显示广告商编辑页面
	 * @param int $id
	 */
	function customer_edit($id)
	{
		$customer_id['id'] = intval($id);
		if (empty($customer_id))
		{
			$this->_show_message(base_url('ad/customer_manage'), $this->lang->line('ad_customerid_unable_empty'), 'goback');
		}
	
		// 查询广告位置信息
		$customer = $this->ad_m->get_ad_customer($customer_id);
		if (empty($customer))
		{
			$this->_show_message(base_url('ad/customer_manage'), $this->lang->line('ad_customer_does_not_exist'), 'goback');
		}
		$customer = $customer[0];
		$customer['begintime'] = date(DATE_FORMAT_AD,$customer['begintime']);
		$customer['finishtime'] = date(DATE_FORMAT_AD,$customer['finishtime']);
		$data = array (
					
				'header' => $this->header,
				'ad_customer' => $customer,
				'title' => $this->lang->line('ad_title_customer_edit'),
				'page_label' => $this->lang->line('ad_page_label'),
				'nav_label' => $this->lang->line('com_navigate_label'),
				'place' => $this->get_ad_place_as_option($customer['placeid'])
		);
		$this->load->view('ad_customer_edit', $data);
	}
	
	/**
	 * 广告商编辑业务处理
	 */
	function customer_edit_action()
	{
		$validate = $this->validation($this->validate_data['customer']);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('ad/customer_manage'), $validate, 'goback');
		}
	
		// 获取表单数据并更新
		$ad_customer = $this->input->post();
		if ($ad_customer && isset($ad_customer['s_ad_customer_edit']))
		{
			$ad_customer_id['id'] = intval($ad_customer['id']);
			unset($ad_customer['s_ad_customer_edit']);
			unset($ad_customer['id']);
			
			// 判断添加的时间，结束时间要大于开始时间
			$ad_customer['begintime'] = strtotime($ad_customer['begintime']);
			$ad_customer['finishtime'] = strtotime($ad_customer['finishtime']);
			if ($ad_customer['begintime'] >= $ad_customer['finishtime'])
			{
				$this->_show_message(base_url('ad/customer_manage'), $this->lang->line('ad_error_time'), 'goback');
			}
			
			$result = $this->ad_m->update_ad_customer($ad_customer_id, $ad_customer);
			$this->_show_message(base_url('ad/customer_manage'), $this->lang->line('msg_operation_success'));
		}
	}
	
	/**
	 * ajax删除广告商
	 */
	function customer_delete_action()
	{
		// 对表单数据做判断
		$validate = $this->validation(array(array('field' => 'id[]', 'label' => 'lang:ad_customer_id', 'rules' => 'trim|required|is_natural|xss_clean')));
		if (TRUE !== $validate)
		{
			echo $this->array2json(TRUE, $validate);
			return ;
		}
	
		// 获取广告商ID值，并放入数组
		$id['id'] = $this->input->post('id');
		if ($this->ad_m->delete_ad_customer($id))
		{
			echo $this->array2json(FALSE, $this->lang->line('msg_operation_success'));
			return ;
		}
		else
		{
			echo $this->array2json(TRUE, $this->lang->line('msg_operation_failed'));
			return ;
		}
	}
	
	/**
	 * 获取广告位，组成option列表形式
	 * @param int $place_id select列表中要选中的广告位
	 * @return string
	 */
	protected function get_ad_place_as_option($place_id = '')
	{
		$place_id = (int)$place_id;
		$option = '';
		$place = $this->ad_m->get_ad_place();
		foreach ($place as $value)
		{
			$selected = $value['id'] == $place_id ? ' selected="selected" ' : '';
			$option .= '<option ' . $selected .' value="' . $value['id'] . '">' . $value['place'] . '</option>';
		}
		return $option;
	}
}

/* End of file ad.php */
/* Location: ./application/controllers/ad.php */